/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2021-01-04
*/
package com.rzico.article.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.article.entity.Talent;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Product;
import com.rzico.basics.entity.Shop;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.ProductService;
import com.rzico.basics.service.ShopService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.article.entity.Recommend;
import com.rzico.article.service.RecommendService;
import io.swagger.models.auth.In;
import net.bytebuddy.asm.Advice;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * 推荐平台控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2021-01-04
 */
@Api(description = "推荐平台接口")
@RestController
@RequestMapping("/admin/recommend")
public class RecommendAdminController extends BaseController {

    @Autowired
    private RecommendService recommendService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private ProductService productService;

    @Autowired
    private ShopService shopService;

    /**
     * 分页查询推荐平台
     *
     * @return
    */
    @ApiOperation("分页查询推荐平台")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "areaId", value = "城市ID", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型{0:商品,1:名店,2:酒店,3:名厨,4.餐车}", dataType = "String", paramType = "query")
    })
    public CommResult<Recommend> list(Date startDate, Date endDate, Integer type,String name, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }
        if (type!=null){
            params.put("type", type);
        }
        if (name!=null){
            params.put("name", name);
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Recommend> list = recommendService.selectList(params);
        PageResult<Recommend> pageResult = new PageResult<Recommend>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 查询单条推荐平台
     *
     * @return
    */
    @ApiOperation("查询单条推荐平台")
    @GetMapping("/find/{id}")
    public CommResult<Recommend> find(@PathVariable String id) {
        Recommend result = recommendService.findById(id);

        Map<String,Object> data = new HashMap<>();
        data.put("recommend",result);
        return CommResult.success(data);

    }

    /**
     * 保存推荐平台
     *
     * @param recommend
     * @return
     */
    @ApiOperation("保存推荐平台")
    @PostMapping("/save")
    public CommResult<Recommend> save(@RequestBody Recommend recommend) {
        if (recommend.getType().equals(0)) {
            Product product = productService.selectByPrimaryKey(recommend.getTypeId());
            Enterprise enterprise = enterpriseService.selectByPrimaryKey(product.getEnterpriseId());
            Shop shop = shopService.findByPoint(enterprise,0d,0d);
            recommend.setLat(shop.getLat());
            recommend.setLng(shop.getLng());
            recommend.setAreaId(shop.getAreaId());
            recommend.setAreaName(shop.getAreaName());
            recommend.setName(product.getName());
            recommend.setOrders(0);
            recommend.setEnterpriseId(product.getEnterpriseId());

        } else {
            Shop shop = shopService.selectByPrimaryKey(recommend.getTypeId());
            recommend.setLat(shop.getLat());
            recommend.setLng(shop.getLng());
            recommend.setAreaId(shop.getAreaId());
            recommend.setAreaName(shop.getAreaName());
            recommend.setName(shop.getName());
            recommend.setOrders(0);
            recommend.setEnterpriseId(shop.getEnterpriseId());
        }

        int affectCount = recommendService.insertUseGeneratedKeys(recommend);
        if (affectCount <= 0) {
            return CommResult.error();
        }
        Recommend result = recommendService.findById(recommend.getId());
        return CommResult.success(result);

    }


    /**
     * 更新推荐平台
     *
     * @param recommend
     * @return
     */
    @Log(desc = "更新推荐平台", type = Log.LOG_TYPE.UPDATE)
    @ApiOperation("更新推荐平台")
    @PostMapping("/update")
    public CommResult<Talent> update(@RequestBody Recommend recommend) {
        int affectCount = recommendService.updateByPrimaryKeySelective(recommend);
        if (affectCount <= 0){
            return CommResult.error();
        }
        Recommend result = recommendService.findById(recommend.getId());
        return CommResult.success(result);
    }


    /**
     * 批量删除推荐平台
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除推荐平台", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除推荐平台,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<Recommend> del(@PathVariable String ids) {

        recommendService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

}
